package com.hxexp.weixin.mvc.trafficline.controller;

import java.util.HashMap;
import java.util.Map;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.hxexp.weixin.common.exception.ServiceException;
import com.hxexp.weixin.constant.DayOfWeek;
import com.hxexp.weixin.constant.SchType;
import com.hxexp.weixin.constant.StartStationsId;
import com.hxexp.weixin.constant.StringConstant;
import com.hxexp.weixin.constant.WebAppVersion;
import com.hxexp.weixin.hpe.api.trafficline.model.TrafficLineParamBean;
import com.hxexp.weixin.hpe.api.trafficline.model.TrafficLineResultBean;
import com.hxexp.weixin.mvc.common.service.CalendarService;
import com.hxexp.weixin.mvc.common.service.CommonSerivce;
import com.hxexp.weixin.mvc.trafficline.model.TicketQueryParam;
import com.hxexp.weixin.mvc.trafficline.service.TrafficlineService;

/**
 * 车次线路controller
 *
 * @author li.jiang
 *
 */
@Controller
@RequestMapping(value="/trafficline")
public final class TrafficlineController {
    private static final Logger LOGGER = LoggerFactory.getLogger(TrafficlineController.class);
    private static final String TICKETQUERYPARAM_ATTR = "ticketQueryParam";
    private static final String YEAR_ATTR = "year";
    private static final String MONTH_ATTR = "month";
    private static final String DAY_ATTR = "day";
    private static final String DAYNAME_ATTR = "dayName";
    @Autowired
    private TrafficlineService trafficlineService;
    @Autowired
    private CalendarService calendarService;
    @Autowired
    private CommonSerivce commonSerivce;
    /**
     * 设置页面需要的常量
     *
     * @return
     */
    @ModelAttribute("constant")
    public Map<String, String> setConstant() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("appver_nc", WebAppVersion.NC);// 南充当代运业
        map.put("appver_lz", WebAppVersion.LZ);// 泸州中心站
        map.put("appver_hx", WebAppVersion.HX);// 华夏快线
        map.put("schType_fixed", SchType.FIXED);// 固定班
        map.put("schType_serial", SchType.SERIAL);// 流水班
        map.put("appver_ylns", WebAppVersion.YLNS); // 四川仪陇农商银行
        map.put("appver_ay", WebAppVersion.AY); // 安岳客运总站
        map.put("startstationid_ay", StartStationsId.AY); // 安岳客运总站编码

        return map;
    }

    /**
     * 车票查询页面
     *
     * @param queryParam
     *            查询参数
     * @param model
     * @return 车票查询页面视图名
     */
    @RequestMapping(value="/index")
    public String index(TicketQueryParam queryParam, Model model) {

        model.addAttribute(TICKETQUERYPARAM_ATTR, queryParam);
        // 设置日历选择的年月日和星期
        if (!StringUtils.isEmpty(queryParam.getLeaveDate())) {
            LocalDate selectLeaveDate = new LocalDate(queryParam.getLeaveDate());
            model.addAttribute(YEAR_ATTR, selectLeaveDate.getYear());
            model.addAttribute(MONTH_ATTR, selectLeaveDate.getMonthOfYear());
            model.addAttribute(DAY_ATTR, selectLeaveDate.getDayOfMonth());
            model.addAttribute(DAYNAME_ATTR, DayOfWeek.getName(selectLeaveDate.getDayOfWeek()));
        } else {
            // 设置日历的默认日期，默认为当天
            LocalDate todayDate = LocalDate.now();
            model.addAttribute(YEAR_ATTR, todayDate.getYear());
            model.addAttribute(MONTH_ATTR, todayDate.getMonthOfYear());
            model.addAttribute(DAY_ATTR, todayDate.getDayOfMonth());
            model.addAttribute(DAYNAME_ATTR, DayOfWeek.getName(todayDate.getDayOfWeek()));
        }

        return "enquiry";
    }

    /**
     * 日历页面
     *
     * @param queryParam
     * @param model
     * @return
     */
    @RequestMapping(value="/calendar")
    public String calendar(TicketQueryParam queryParam, String isRoll, Model model) {
        // 初始化日历页面的变量
        calendarService.initCalendar(queryParam.getLeaveDate(), isRoll, model);
        model.addAttribute(TICKETQUERYPARAM_ATTR, queryParam);

        return "calendar";
    }

    /**
     * 提交参数查询车票
     *
     * @param queryParam
     * @param model
     * @return
     */
    @RequestMapping(value="/enquiry")
    public String enquiry(TicketQueryParam queryParam, Model model) {
        TrafficLineResultBean trafficLineResultBean = new TrafficLineResultBean();
        TrafficLineParamBean trafficLineParamBean = new TrafficLineParamBean();

        // 设置查询车次的参数值
        trafficLineParamBean.setStartCityId(queryParam.getStartCityId());
        trafficLineParamBean.setStartStationId(queryParam.getStartStationId());
        trafficLineParamBean.setArriveCityId(queryParam.getArriveCityId());
        trafficLineParamBean.setArriveStationId(queryParam.getArriveStationId());
        trafficLineParamBean.setLeaveDate(queryParam.getLeaveDate());
        trafficLineParamBean.setGroup(StringConstant.BLANK);
        trafficLineParamBean.setParam2(StringConstant.BLANK);
        trafficLineParamBean.setParam3(StringConstant.BLANK);
        try {
            trafficLineResultBean = trafficlineService.queryTrafficLine(trafficLineParamBean);
        } catch (ServiceException e) {
            LOGGER.error("查询车次发生异常", e);
        }

        // 把日期字符串转换成LocalDate对象，方便日期计算
        LocalDate localDate = LocalDate.parse(queryParam.getLeaveDate());
        model.addAttribute(YEAR_ATTR, localDate.getYear());
        model.addAttribute(MONTH_ATTR, localDate.getMonthOfYear());
        model.addAttribute(DAY_ATTR, localDate.getDayOfMonth());
        model.addAttribute("lastYMD", localDate.minusDays(1).toString());
        model.addAttribute("nextYMD", localDate.plusDays(1).toString());

        model.addAttribute(TICKETQUERYPARAM_ATTR, queryParam);
        model.addAttribute("trafficLineList", trafficLineResultBean.getList());
        model.addAttribute("now", DateTime.now().toString());
        model.addAttribute("isNCStartStation", commonSerivce.isNCStartStation(queryParam.getStartCityId(), queryParam.getStartStationId()));
        model.addAttribute("isXIANStation", commonSerivce.isXAStartStation(queryParam.getStartCityId(), queryParam.getStartStationId()));
        model.addAttribute("isZIYANGStation", StartStationsId.ZY.equals(queryParam.getStartStationId()));
        model.addAttribute("isXUANHANStation", StartStationsId.XH.equals(queryParam.getStartStationId()));
        model.addAttribute("isJIANYANGStation", StartStationsId.JY.equals(queryParam.getStartStationId()));
        model.addAttribute("isHeJiangYiYuanJieStation", StartStationsId.HJ.equals(queryParam.getStartStationId()));
        model.addAttribute("isBYStartStation", commonSerivce.isBYStartStation(queryParam.getStartStationId()));
        model.addAttribute("isBJKYZStation", StartStationsId.BJKYZ.equals(queryParam.getStartStationId()));

        return "trafficline_list";
    }
}
